import MySQL
import linecache

p_id = 0


def bkd_hash(s):
    hash = 0
    seed = 131
    for ch in s:
        hash = (hash * seed + ord(ch)) % 10000003
    return hash


def resolve(file_name):
    global p_id
    objs = []
    date = file_name.split("-")[1]
    hour = int(file_name.split("-")[2]) // 10000
    with open(file_name, 'r', encoding='UTF-8') as f:
        rows = f.readlines()
        for row in rows:
            p_id += 1
            tuple = []
            tmp = ''
            for ch in row:
                if ch == ' ' or ch == '\n' or ch == '\r\n':
                    tuple.append(tmp)
                    tmp = ''
                else:
                    tmp += ch
            project = tuple[0]
            page = str(hex(bkd_hash(tuple[1])))[2:]
            count = int(tuple[2])
            size = min(int(tuple[3]), 299999999)
            objs.append({
                'id': p_id,
                'date': date,
                'hour': hour,
                'project': project,
                'page': page,
                'count': count,
                'size': size
            })
            if p_id % 1000 == 0:
                MySQL.insert('raw_pagecount', objs)
                objs = []
        MySQL.insert('raw_pagecount', objs)


def resolve_line_numbers(day, hour, pre_count):
    file_name = '../data/wiki/raw/pagecounts-{year}{month}{day}-{hour}0000'.format(
        year=2016,
        month='%02d' % 1,
        day='%02d' % day,
        hour='%02d' % hour
    )
    current_count = 0
    fp = open(file_name, "r", encoding='utf-8')
    while 1:
        buffer = fp.read(8 * 1024 * 1024)
        if not buffer:
            break
        current_count += buffer.count('\n')
    total_count = current_count + pre_count
    MySQL.update('aqp_result',
                 {'size': total_count},
                 '`table`=\'hour_count\' AND `date`=\'201601' + ('%02d' % day) + '\' AND `hour`=' + str(hour)
                 )
    return total_count


if __name__ == '__main__':
    MySQL.init('wiki')
    count = 0
    for day in range(1, 8):
        for hour in range(24):
            count = resolve_line_numbers(day, hour, count)
            # for year in range(2016, 2017):
            #     for month in range(1, 2):
            #         for day in range(1, 2):
            #             for hour in range(24):
            #                 resolve('../data/wiki/raw/pagecounts-{year}{month}{day}-{hour}0000'.format(
            #                     year=year,
            #                     month='%02d' % month,
            #                     day='%02d' % day,
            #                     hour='%02d' % hour
            #                 ))
